﻿namespace RSTAi.Platform.GrpcService.Common
{
    /// <summary>
    /// uuid 生成ID
    /// </summary>
    public class UUID
    {
        private static Random random = new Random();

        private static uint GenerateUUID()
        {
            byte[] buffer = new byte[16];
            random.NextBytes(buffer);
            uint time = (uint)DateTime.UtcNow.Ticks << 32;
            time |= (uint)buffer[6];
            time <<= 16;
            time |= (uint)buffer[4];
            uint node = (uint)buffer[0] << 24 | (uint)buffer[1] << 16 | (uint)buffer[2] << 8 | (uint)buffer[3];
            uint pid = (uint)buffer[5];
            return ((time << 16) | node) | ((pid << 48) >> 16);
        }

        public static long GenerateLongUUID()
        {
            uint uuid = GenerateUUID();
            long longUUID = ((long)uuid << 32) | (long)(uuid >> 32);
            return longUUID;
        }
    }
}
